<?php
class ControllerFeedNewsGoogleSitemap extends Controller {
   public function index() {
	  if ($this->config->get('news_google_sitemap_status')) {
		 $output  = '<?xml-stylesheet type="text/xsl" href="catalog/view/javascript/news/feed/gss.xsl"?>'. "\n";
		 $output .= '<urlset xmlns="http://www.google.com/schemas/sitemap/0.84">'. "\n";
		 
		 $this->load->model('news/article');
		 
		 $articles = $this->model_news_article->getArticles();
		 
		 foreach ($articles as $article) {
			$output .= '<url>'. "\n";
			$output .= '<loc>' . $this->url->link('news/article', 'article_id=' . $article['article_id']) . '</loc>'. "\n";
		 	$output .= '<lastmod>' . date('Y-m-d', strtotime($article['date_modified'])) . '</lastmod>'. "\n";
			$output .= '<changefreq>weekly</changefreq>'. "\n";
			$output .= '<priority>1.0</priority>'. "\n";
			$output .= '</url>'. "\n";
		 }
		 
			 $this->load->model('news/category');		 
			 $output .= $this->getCategories(0);
			 
		 $this->load->model('news/author');
		 
		 $authors = $this->model_news_author->getAuthors();
		 foreach ($authors as $author) {
			$output .= '<url>'. "\n";
			$output .= '<loc>' . $this->url->link('news/author/info', 'author_id=' . $author['author_id']) . '</loc>'. "\n";
			//$output .= '<lastmod></lastmod>'. "\n";
			$output .= '<changefreq>weekly</changefreq>'. "\n";
			$output .= '<priority>0.7</priority>'. "\n";
			$output .= '</url>'. "\n";         
		 }
		 
		 
			$output .= '<url>'. "\n";
			$output .= '<loc>' . $this->url->link('news/author') . '</loc>'. "\n";
			$output .= '<changefreq>weekly</changefreq>'. "\n";
			$output .= '<priority>1.0</priority>'. "\n";
			$output .= '</url>'. "\n";  
		 
		 $output .= '</urlset>'. "\n";
		 
		 $this->response->addHeader('Content-Type: application/xml');
		 $this->response->setOutput($output);
	  }
   }
   
   protected function getCategories($parent_id, $current_cpath = '') {
	  $output = '';
	  
	  $results = $this->model_news_category->getCategories($parent_id);
	  
	  foreach ($results as $result) {
		 if (!$current_cpath) {
			$new_cpath = $result['article_category_id'];
		 } else {
			$new_cpath = $current_cpath . '_' . $result['article_category_id'];
		 }

		 $output .= '<url>'. "\n";
		 $output .= '<loc>' . $this->url->link('news/category', 'cpath=' . $new_cpath) . '</loc>'. "\n";
		 $output .= '<lastmod>' . date('Y-m-d', strtotime($result['date_modified'])) . '</lastmod>'. "\n";
		 $output .= '<changefreq>weekly</changefreq>'. "\n";
		 $output .= '<priority>0.7</priority>'. "\n";
		 $output .= '</url>'. "\n";         
		 
		   $output .= $this->getCategories($result['article_category_id'], $new_cpath);
	  }

	  return $output;
   }      
}
?>